﻿using CCL.Utility;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Server.Service
{
    /// <summary>
    /// 프로젝트 멤버 리스트 요청에 대한 서비스를 제공합니다.
    /// </summary>
    public class RegistProjectMemberList : IService
    {
        /// <summary>
        /// 요청받은 메시지의 본문
        /// </summary>
        CCL.SocketMessage.Request.RegistProjectMemberList m_body;

        /// <summary>
        /// 새로운 서비스를 생성합니다.
        /// </summary>
        /// <param name="body">요청받은 메시지 본문</param>
        public RegistProjectMemberList(CCL.SocketMessage.Request.RegistProjectMemberList body)
        {
            m_body = body;
        }

        /// <summary>
        /// 서비스를 시작합니다.
        /// </summary>
        /// <returns>전송할 byte배열</returns>
        public byte[] Execute()
        {
            byte[] returnBytes = null;
            using (SqlConnection conn = new SqlConnection(Properties.Resources.ConnectionString))
            {
                try
                {
                    conn.Open();

                    foreach (CCL.SocketMessage.Request.RegistProjectMemberList.Member member in m_body.MemberList)
                    {
                        int userId = 0;

                        SqlCommand sqlCmd;

                        sqlCmd = new SqlCommand(@"SELECT userId FROM [User] WHERE email = @email", conn);
                        sqlCmd.Parameters.AddWithValue("@email", member.Email);

                        using (SqlDataReader reader = sqlCmd.ExecuteReader())
                        {
                            reader.Read();

                            if (reader.HasRows)
                                userId = (int)reader["userId"];
                            else
                                continue;
                        }

                        sqlCmd = new SqlCommand(@"
                            INSERT INTO [ProjectMember](projectId, userId, permission)
                            VALUES (@pid, @uid, @perm)", conn);

                        sqlCmd.Parameters.AddWithValue("@pid", m_body.ProjectID);
                        sqlCmd.Parameters.AddWithValue("@uid", userId);
                        sqlCmd.Parameters.AddWithValue("@perm", member.Permission);

                        sqlCmd.ExecuteNonQuery();
                    }

                    CCL.SocketMessage.Response.RegistProjectMemberList resp = new CCL.SocketMessage.Response.RegistProjectMemberList();
                    CCL.SocketMessage.Message respMsg = CCL.SocketMessage.Message.MessageResponseBuilder(resp);

                    returnBytes = respMsg.GetByte();
                }
                catch (System.Exception ex)
                {
                    CCL.SocketMessage.Response.RegistProjectMemberList resp = new CCL.SocketMessage.Response.RegistProjectMemberList(ex.Message);
                    CCL.SocketMessage.Message respMsg = CCL.SocketMessage.Message.MessageResponseBuilder(resp);

                    returnBytes = respMsg.GetByte();
                }
            }

            return returnBytes;
        }
    }
}
